/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * BaseFlowRoleController
 * 流程角色
 * 修改纪录
 * 2021-01-11 版本：1.0 wanghanwei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：wanghanwei
 * 创建日期：2021-01-11
 */
package com.topscomm.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.base.BaseSystemConst;
import com.topscomm.base.controller.auto.BaseFlowRoleControllerAuto;
import com.topscomm.base.service.impl.BaseFlowRoleUserService;
import com.topscomm.cbo.model.TreeModel;
import com.topscomm.cbo.pojo.CboApprovalFlowEntity;
import com.topscomm.cbo.service.ICboApprovalFlowService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: wanghanwei
 * @date: 2021-01-11
 * @description:流程角色Controller
 * @modify:
 */
@RestController
@RequestMapping("base/flowRole")
public class BaseFlowRoleController extends BaseFlowRoleControllerAuto implements IBaseFlowRoleController {
	@Autowired
	private ICboApprovalFlowService cboApprovalFlowService;
	@Autowired
	private BaseFlowRoleUserService baseFlowRoleUserService;

	/**
	 * @author: wanghanwei
	 * @date: 2021-01-11
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_parentid")) {
			String value = paraMap.get("qry_parentid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and parentid = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_flowid")) {
			String value = paraMap.get("qry_flowid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and flowid = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @description: 参照查询方法
	 * @param paraMap
	 * @return
	 * @author: wanghanwei
	 * @date: 2021-01-11
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description: 树查询方法
	 * @param paraMap
	 * @return
	 * @author: wanghanwei
	 * @date: 2021-01-11
	 * @modify:
	 */
	@GetMapping(value = "/queryTreeList")
	public ResponseResult<Object> queryTreeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			StringBufferProxy sql = new StringBufferProxy();
			sql.appendSingle(" category is null order by code asc");
			List<CboApprovalFlowEntity> cboApprovalFlowEntities = cboApprovalFlowService.queryByWhere(sql.toString());
			List<CboApprovalFlowEntity> MCRList = cboApprovalFlowEntities.stream()
					.filter(item -> BaseSystemConst.MCR_APPROVAL_CODE.equals(item.getCode()))
					.collect(Collectors.toList());
			if (CollectionUtils.isEmpty(MCRList)) {
				return ResponseResult.error("流程分类中没有MCR大流程对应分类");
			}
			CboApprovalFlowEntity cboApprovalFlowEntity = MCRList.get(0);
			long parentId = cboApprovalFlowEntity.getId();
			// 查询所有mcr的流程
			sql.clear();
			sql.appendSingle(" parentid = {0}", parentId);
			sql.appendSingle(" and category is not null");
			List<CboApprovalFlowEntity> mcrApprovalFlowEntities = cboApprovalFlowService.queryByWhere(sql.toString());
			List<Map<String, Object>> mcrApprovalFlows = mcrApprovalFlowEntities.stream()
					.map(CboApprovalFlowEntity::convertToMap).collect(Collectors.toList());
			List<Map<String, Object>> listNewModule = new ArrayList<Map<String, Object>>();
			listNewModule.addAll(mcrApprovalFlows);
			listNewModule.add(cboApprovalFlowEntity.convertToMap());
			TreeModel treeModel = new TreeModel();
			List<TreeModel> treeList = treeModel.buildTree(listNewModule, true, "流程角色树");
			Map<String, Object> dataMap = new HashMap<>();
			dataMap.put("dataList", treeList);
			return ResponseResult.ok(dataMap);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/**
	 * @description:组件查询接口
	 * @auther: wanghanwei
	 * @date: 2021年1月15日下午8:09:46
	 * @modify:
	 */
	@GetMapping(value = "/queryElementList")
	public ResponseResult<Object> queryElementList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			List<Map<String, Object>> list = this.service.queryRoleUserList(paraMap);
			return ResponseResult.ok(list);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @description:角色删除的同时删除角色下人员
	 * @auther: wanghanwei
	 * @date: 2021年2月4日上午11:33:16
	 * @modify:
	 */
	@Override
	@DeleteMapping(value = "/delete")
	public ResponseResult<Object> delete(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String ids = ConvertUtil.convertToString(paraMap.get("ids"));
			this.service.deleteByWhere((new StringBuilder(" id in (")).append(ids).append(")").toString());
			baseFlowRoleUserService
					.deleteByWhere((new StringBuilder(" roleid in (")).append(ids).append(")").toString());
			afterDelete(paraMap);
			return ResponseResult.ok("删除成功");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	@Override
	protected void beforeInsert(Map<String, Object> map, Map<String, Object> map1) throws Exception {
		map.put("parentid", map.get("flowid"));
	}
}
